﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class MachineController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IMachineRepository MachineRepository;
        public MachineController(ICommandBus commandBus, IMachineRepository MachineRepository)
        {
            ModelState.Clear();
            this.commandBus = commandBus;
            this.MachineRepository = MachineRepository;
           
        }
        //
        // GET: /Machine/
        [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = MachineRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Create()
        {
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult Save(MachineFormModels frm)
        {
            var form = new MachineFormModels()
            {
                ID = frm.ID,
                Code = frm.Code,
                Description = frm.Description,
                Name = frm.Name,
            };
            var command = Mapper.Map<MachineFormModels, CreateOrUpdateMachineCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);

            }
            var model = MachineRepository.GetAll();
            return PartialView("_MachineList", model.ToPagedList(1, 10));
        }

        [HttpGet]
        public JsonResult Get(int ID)
        {
            Machine product = MachineRepository.GetById(ID);
            var obj = Mapper.Map<Machine, MachineFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteMachineCommand> list = new List<DeleteMachineCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteMachineCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = MachineRepository.GetAll();
            return PartialView("_MachineList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<Machine> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = MachineRepository.GetAll();
            }
            else
            {
                model = MachineRepository.GetMany(x => (x.Name.Contains(Name)) || (x.Description.Contains(Name)));

            }
            return PartialView("_MachineList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = MachineRepository.GetAll();
            return PartialView("_MachineList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
